package com.qkst.website.web.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qkst.website.common.utils.MD5Utils;
import com.qkst.website.common.utils.StringUtils;
import com.qkst.website.web.bean.BargainLogBean;
import com.qkst.website.web.mapper.BargainLogMapper;
import com.qkst.website.web.service.BargainService;

/**
 * @author lihao
 * @description 砍价service
 * @date 2017-05-08
 * @email ahlxlihao@163.com
 *
 */
@Service("bargainService")
@Transactional
public class BargainServiceImpl implements BargainService {
	@Autowired
	private BargainLogMapper bargainMapper;
	/**
	 * 根据砍价条件查询砍价记录
	 * @param order
	 * @return List<Order>
	 */
	public List<BargainLogBean> queryBargainLogList(BargainLogBean bargainLog){
		//分页计算
		return bargainMapper.selectBargainLogList(bargainLog);
	}
	
	/**
	 * 查询总记录数
	 * @param order
	 * @return 
	 */
	@Override
	public int queryCount(BargainLogBean bargainLog) {
		return bargainMapper.selectCount(bargainLog);
	}

	
	/**
	 * 创建砍价记录
	 * @param bargainLog
	 * @return boolean
	 */
	@Override
	public void save(BargainLogBean bargainLog,Map<String, Object> result) {
		Short status=0;//设置状态为正在砍价
		bargainLog.setStatus(status);
		//检查是否已经创建过砍价链接
		List<BargainLogBean> bargainLogList=bargainMapper.selectBargainLogList(bargainLog);
		if(bargainLogList==null || bargainLogList.isEmpty()){//如果没有生成过
			String tokenId=bargainLog.getBuyerId()+"_"+bargainLog.getProduct().getProductId()+"_"+bargainLog.getProduct().getPartnum();//明文
			String key=StringUtils.randomInt(1000,10000)+"";//密钥
			String token=MD5Utils.getMD5Str("tokenId="+tokenId+"&key="+key);//加密后
			bargainLog.setCreateTime(new Date());
			bargainLog.setLastUpdate(new Date());
			bargainLog.setKey(key);
			bargainLog.setTokenId(tokenId);
			bargainLog.setToken(token);
			bargainLog.setAmount(BigDecimal.ZERO);//初始化砍价金额
			bargainLog.setTimes(0);//初始化砍价次数
			bargainLog.setProductId(bargainLog.getProduct().getProductId());
			int num=bargainMapper.insert(bargainLog);//插入砍价记录
			result.put("bargainLog",bargainLog);
			if(num>0){//插入成功
				result.put("isSuccess", true);
				result.put("code", "000");
			}else{//插入失败
				result.put("isSuccess", false);
				result.put("code", "500");
			}
		}else{
			result.put("bargainLog",bargainLogList.get(0));
			result.put("isSuccess", true);
			result.put("code", "000");
		}
		
	}
	
	/**
	 * 删除砍价记录
	 * @param bargainLog
	 * @param result
	 * @return boolean
	 */
	@Override
	public void delete(BargainLogBean bargainLog, Map<String, Object> result) {
		if(bargainLog.getBargainId()!=null){
			Short status=-1;//删除
			bargainLog.setStatus(status);
			bargainLog.setLastUpdate(new Date());
			int num=bargainMapper.updateByPrimaryKeySelective(bargainLog);
			result.put("bargainLog",bargainLog);
			if(num>0){//删除成功
				result.put("isSuccess", true);
				result.put("code", "000");
			}else{//删除失败
				result.put("isSuccess", false);
				result.put("code", "500");
			}
		}else{
			result.put("isSuccess", false);
			result.put("code", "404");
		}
		
	}

	@Override
	public BargainLogBean queryBargainLogByConditions(BargainLogBean bargain) {
		bargain=bargainMapper.sumBargainPriceByConditions(bargain);
		return bargain;
	}

	//根据token查询Bargain
	@Override
	public BargainLogBean queryBargainLogByToken(String token) {
		BargainLogBean bargainlog = new BargainLogBean();
		bargainlog=bargainMapper.selectBargainLogByToken(token);
		return bargainlog;
	}
	
}
